//
//  WmcCancellationReasonTextView.swift
//  YPWatermarkCamera
//
//  Created by 马涛 on 2022/6/21.
//

import UIKit

class WmcCancellationReasonTextView: WmcTextView {

    var maxHeight: CGFloat =  120
    
    override func makeUI() {
        super.makeUI()

        self.backgroundColor = UIColor.wmc_white_FFFFFF
        self.textView.backgroundColor = UIColor.wmc_white_FFFFFF
        self.countLabel.text = "0/300"
        
        
        self.clearBtn.snp.removeConstraints()
        self.clearBtn.removeFromSuperview()

        self.countLabel.snp.remakeConstraints { make in
            make.right.equalTo(-13)
            make.bottom.equalToSuperview()
        }
    }
    
    override func bindViewModel() {
        super.bindViewModel()
        /// 文本变化更新约束
        self.textView.rx.text.skip(1).bind {[weak self] text in
            guard let weakSelf = self else { return }
            weakSelf.textView.snp.remakeConstraints { make in
                make.top.left.right.equalToSuperview()
                make.height.greaterThanOrEqualTo(weakSelf.computeTextViewHeight())
                //make.height.equalTo(self.computeTextViewHeight())
                make.bottom.equalTo(-37)
            }
        }.disposed(by: rx.disposeBag)
    }
    
    /// 计算文本高度
    /// - Returns: description
    func computeTextViewHeight() -> CGFloat {
        let constrainSize = CGSize(width: self.textView.frame.size.width, height: CGFloat.greatestFiniteMagnitude)
        let size = self.textView.sizeThatFits(constrainSize)
        if size.height >= maxHeight {
            self.textView.isScrollEnabled = false
        } else {
            self.textView.isScrollEnabled = false
            return maxHeight
        }
        return size.height
    }
}
